<?php

namespace app\api\controller\v1;

use app\BaseController;
use app\api\model\AdminModel;
use think\facade\Db;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;
use app\middleware\ApiAuth;
use app\api\model\UserModel;

/**
 * Class 微信授权 用户端
 * @package app\controller
 */
class Wxlogin extends BaseController
{
    protected $middleware = [ApiAuth::class];
    /**
     * @desc 微信授权回调接口地址
     */
    public function login(Request $request)
    {
        // 获取Access_token
        $input['state'] = $request->param('state');//原样参数返回..
        $input['code'] = $request->param('code');//code
        $input['admin_id'] = $request->param('admin_id');//code
        // 参数校验
        $validate = Validate::rule([
            'code' => ValidateRule::isRequire(),
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return UserModel::Wxlogin($input);
    }

    /**
     * @desc 根据运营商获取APPID
     * @return Json
     */
    public function GetAppid(Request $request)
    {
        $input['admin_id'] = $request->param('admin_id');//运营商ID
        // 参数校验
        $validate = Validate::rule([
            'admin_id' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return AdminModel::GetAppid($input);
    }
}
